package com.example.backed.controller;

import com.example.backed.dto.CartItemAddDTO;
import com.example.backed.entity.CartItem;
import com.example.backed.service.CartService;
import com.example.backed.util.SecurityUtil;
import com.example.backed.vo.CartStatsVO;
import com.example.backed.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "购物车管理")
@RestController
@RequestMapping("/api/cart")
@RequiredArgsConstructor
@Validated
public class CartController {

    private final CartService cartService;

    @Operation(summary = "添加商品到购物车")
    @PostMapping("/items")
    public Result<CartItem> addToCart(@Valid @RequestBody CartItemAddDTO addDTO) {
        Long userId = SecurityUtil.getCurrentUserId();
        CartItem cartItem = cartService.addToCart(userId, addDTO);
        return Result.success(cartItem);
    }

    @Operation(summary = "更新购物车项数量")
    @PutMapping("/items/{id}/quantity")
    public Result<Boolean> updateQuantity(
            @Parameter(description = "购物车项ID") @PathVariable Long id,
            @Parameter(description = "数量") @RequestParam @Min(1) Integer quantity) {
        Long userId = SecurityUtil.getCurrentUserId();
        boolean success = cartService.updateQuantity(userId, id, quantity);
        return success ? Result.success(true) : Result.error("更新数量失败");
    }

    @Operation(summary = "更新购物车项选中状态")
    @PutMapping("/items/{id}/selected")
    public Result<Boolean> updateSelected(
            @Parameter(description = "购物车项ID") @PathVariable Long id,
            @Parameter(description = "选中状态：0-未选中，1-已选中") @RequestParam Integer selected) {
        Long userId = SecurityUtil.getCurrentUserId();
        boolean success = cartService.updateSelected(userId, id, selected);
        return success ? Result.success(true) : Result.error("更新选中状态失败");
    }

    @Operation(summary = "删除购物车项")
    @DeleteMapping("/items/{id}")
    public Result<Boolean> deleteCartItem(
            @Parameter(description = "购物车项ID") @PathVariable Long id) {
        Long userId = SecurityUtil.getCurrentUserId();
        boolean success = cartService.deleteCartItem(userId, id);
        return success ? Result.success(true) : Result.error("删除购物车项失败");
    }

    @Operation(summary = "获取购物车列表")
    @GetMapping("/items")
    public Result<List<CartItem>> getCartItems() {
        Long userId = SecurityUtil.getCurrentUserId();
        List<CartItem> cartItems = cartService.getUserCartItems(userId);
        return Result.success(cartItems);
    }

    @Operation(summary = "获取已选中的购物车项")
    @GetMapping("/items/selected")
    public Result<List<CartItem>> getSelectedCartItems() {
        Long userId = SecurityUtil.getCurrentUserId();
        List<CartItem> cartItems = cartService.getSelectedCartItems(userId);
        return Result.success(cartItems);
    }

    @Operation(summary = "清空购物车")
    @DeleteMapping("/items")
    public Result<Boolean> clearCart() {
        Long userId = SecurityUtil.getCurrentUserId();
        boolean success = cartService.clearCart(userId);
        return success ? Result.success(true) : Result.error("清空购物车失败");
    }

    @Operation(summary = "删除已选中的购物车项")
    @DeleteMapping("/items/selected")
    public Result<Boolean> deleteSelectedItems() {
        Long userId = SecurityUtil.getCurrentUserId();
        boolean success = cartService.deleteSelectedItems(userId);
        return success ? Result.success(true) : Result.error("删除已选中的购物车项失败");
    }

    @Operation(summary = "获取购物车统计信息")
    @GetMapping("/stats")
    public Result<CartStatsVO> getCartStats() {
        Long userId = SecurityUtil.getCurrentUserId();
        CartStatsVO stats = cartService.getCartStats(userId);
        return Result.success(stats);
    }
} 